--假设有部门表和员工表，在添加员工的时候，该员工的部门编号如果在部门表中找不到，则自动添加部门信息，部门名称为"新部门"。
create trigger huhu
on people for insert
as
	insert into Department values ((select DepartmentId from inserted),'新部门')
go

insert into People(DepartmentId,PeopleName,PeopleSex,PeopleSalary) values ('123','咕咕','男',200)
--触发器实现，删除一个部门的时候将部门下所有员工全部删除。
create trigger huahua
on department for delete
as
	delete from people where DepartmentId=(select DepartmentId from deleted)
go
	
delete from Department where DepartmentId='123'
--修改一个部门编号之后，将该部门下所有员工的部门编号同步进行修改
create trigger gugu
on department for update
as
	update People set DepartmentId=(select DepartmentId from inserted) where DepartmentId=(select DepartmentId from deleted)
go

update Department set DepartmentId='004' where DepartmentId='123'